#!/bin/bash

finish() {
    echo '\n🍺   完成'
    exit 0
}

getInfo() {
  info=$1
  yn=$2
  if [ $yn == 1 ]; then
    read -p "${info}[y/n]?" -n 1 result
    echo $result
  else
    read -p "${info}" result
    echo $result
  fi
}

GO_NEXT=`getInfo '此脚本用于初始化 Android 代理机环境，请使用代理机账号运行，确定要继续执行' 1`

if [[ $GO_NEXT != 'y' ]]; then
finish
fi

URL=$0

CURRENT_USER=`whoami`

echo '\n'

# BASE_DIR=`getInfo '请输入项目安装目录:~/' 0`

USER_DIR=/Users/${CURRENT_USER}
cd ${USER_DIR}

# 授权
if [ ! -d ${USER_DIR}/.ssh ];
then
  mkdir -p ${USER_DIR}/.ssh
fi
cat << EOF > ${USER_DIR}/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7mjniEfzB6joKwMrQkc247ww58I5vger47VFmnjrJ27gNAd3ohFpLNzPwcA+YRxQ+3SNEvoup1NSsb/VOQh8EWRDHKCTY/DnVxCVQxcImlUH17Pcj8bM+b3XnsSA+yBRDOmlIa8DvzyKVuaUcH4J6bzHhNRxyoS0FeFHwNteILIvJ/k44MK1XDdEEyluV5qogeozoQ7F+43pzVQaa9iJ5AhjFIKKqeey3L60SjvWDf+7568pZWe7FwBI169x2y6Ypu/reAnMRytvY8QWbJ0njxRm38/+WAfp0RD56qROZbjDE/LUiRLRUziWfbASYyPWyOzv9tmtHQe/nRFYeS7WD kevin@localhost
EOF

START_DIR='iPack'

if [ -d ${USER_DIR}/$START_DIR/Android ];
then
  CLR_DIR=`getInfo "${USER_DIR}/iPack/Android 文件夹已存在，是否覆盖安装" 1`
  if [[ $CLR_DIR != 'y' ]]; then
    echo "\n如需安装请删除${USER_DIR}/iPack/Android 后重新运行本脚本"
    finish
  else
    rm -rf ${USER_DIR}/$START_DIR/Android
  fi
fi

for DIR in ${USER_DIR}/$START_DIR/Android/Components ${USER_DIR}/$START_DIR/Android/Packages ${USER_DIR}/$START_DIR/Android/Archive ${USER_DIR}/$START_DIR/Android/Scripts
do
if [ ! -d $DIR ];
then
   echo '\n创建文件夹 '$DIR
  mkdir -p $DIR
fi
done

BASE_DIR=${USER_DIR}/$START_DIR/Android

# 生成plist文件  在替换对应的路径中替换为你们的路径
cat << EOF > ${BASE_DIR}/Plist/options.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>compileBitcode</key>
    <false/>
    <key>method</key>
    <string>ad-hoc</string>
</dict>
</plist>
EOF

echo '\n获取 Android 打包脚本'
cd ${BASE_DIR}/Scripts
curl -o archive.sh ${URL}'/scripts/archive-android.sh'
chmod +x archive.sh

finish